package com.smarttest.common.beans.protolcol;

import java.io.Serializable;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

/**
 * GPS设备状态
 * @author harry.min(minzaohua@chinawayltd.com)
 *
 */
public class GpsStatus implements Cloneable,Serializable{
	/**  */
	private static final long serialVersionUID = 3042247995798578237L;
	// 定义设备状态常量 0/断线, 1/在线, 2/心跳
	public static final int STATUS_OFFLINE = 0;
	public static final int STATUS_ONLINE = 1;
	public static final int STATUS_HEARTBEAT = 2;
	
	private String imei;
	private int gpsno;
	private int status;			// 状态：0/断线、1/在线、2/心跳
	private int seqnum;			// 流水号
	private int gps=0;			// 是否定位：0/未定位、1/已定位
	private int gsm=-1;			// GSM信号：-1/无此数据、其他/信号强度
	private int acc=-1;			// ACC状态：-1/无此数据、0/ACC关、1/ACC开
	private int battery=-1;		// 电量：-1/无此数据
	private int voltage=-1;		// 电压*10：-1/无此数据
	private int powerAlarm=-1;	// 断电报警：-1/无此数据、0/无报警、1/报警
	private int gnnsAlarm=-1;	// 定位天线报警：-1/无此数据、0/无报警、1/报警

	private int batteryVol=-1;//单位0.1V,smart内部电池电压,-1/无此数据.数据来自properties，batteryVol
	private int mainVol=-1;//单位 0.1V,smart主电电压,-1/无此数据。数据来自properties，mainVol
	
	private String properties;	// 其它状态，根据设备不同而不同
	private Location loc;
	
	protected int confirmAlarm;	// 是否需要确认报警
	private int lbsStatus=-1;//lbs定位标识
	private int turnRightTimes=-1;//向右转次数
	private int turnleftTimes=-1;//向左转次数
	private Date time = new Date();
	
	public GpsStatus() {
	}
	
	public GpsStatus(String imei, int status, String properties) {
		this.imei = imei;
		this.status = status;
		this.properties = properties;
	}
	
	public GpsStatus(String properties) {
		this.properties = properties;
		fromProperties(properties);
	}
	
	public void fromProperties(String properties){
		if (StringUtils.isNotBlank(properties)) {
			String str = properties + ",";
			setAcc(getInt(str, "acc"));
			setGps(getInt(str, "gps"));
			setGsm(getInt(str, "gsm"));
			setBattery(getInt(str, "bp"));
			setBatteryVol(getInt(str, "batteryVol"));
			setMainVol(getInt(str, "mainVol"));
			String szTemp = StringUtils.substringBetween(str, "vt:", ",");
			if (StringUtils.isNotBlank(szTemp)){
				setVoltage((int) (Double.valueOf(szTemp) * 10));
			}
			
			int arr = getInt(str, "ar");
			if (arr > -1) {
				setPowerAlarm((arr == 10 ? 1 : 0));
			} else {
				setGnnsAlarm(getBit(str, "ar1", 5));
				setPowerAlarm(getBit(str, "ar2", 0));
			}
		}
	}
	
	private static int getBit(String str, String tag, int idx) {
		int result = -1;
		String szTemp = StringUtils.substringBetween(str, tag+":", ",");
		if (StringUtils.isNotBlank(szTemp)) {		
			int len = szTemp.length();
			if (len > idx)
				result = Integer.valueOf("" + szTemp.charAt(len-idx-1));
		}
		return result;
	}
	private static int getInt(String str, String tag) {
		int result = -1;
		String szTemp = StringUtils.substringBetween(str, tag+":", ",");
		if (StringUtils.isNotBlank(szTemp)){
			if(StringUtils.isNumeric(szTemp)){
				result = Integer.valueOf(szTemp);
			}else{
				result = Double.valueOf(szTemp).intValue();
			}
		}
		return result;
	}
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("GpsStatus:{");
		sb.append("imei:").append(imei).append(",");
		sb.append("status:").append(status).append(",");
		sb.append("properties:").append(properties);
		sb.append("}");
		return sb.toString();
	}
	
	@Override
	public boolean equals(Object obj) {
		boolean result = (obj == this);
		if (!result && obj instanceof GpsStatus)
			result = (imei.equals(((GpsStatus) obj).getImei()));
		return result;
	}
	
	@Override
	public int hashCode() {
		return imei.hashCode();
	}
	
	/**
	 * 只保留3位数
	 * @param voltage
	 */
	public void setVoltage(int voltage) {
		this.voltage = voltage % 1000;
	}
	
	public String getImei() {
		return imei;
	}
	
	public int getGpsno() {
		return gpsno;
	}

	public void setGpsno(int gpsno) {
		this.gpsno = gpsno;
	}

	public int getStatus() {
		return status;
	}
	public void setImei(String imei) {
		this.imei = imei;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	public String getProperties() {
		return properties;
	}
	public void setProperties(String properties) {
		this.properties = properties;
	}
	public Date getTime() {
		return time;
	}
	public void setTime(Date time) {
		this.time = time;
	}
	public int getGps() {
		return gps;
	}
	public int getGsm() {
		return gsm;
	}
	public int getAcc() {
		return acc;
	}
	public int getBattery() {
		return battery;
	}
	public int getPowerAlarm() {
		return powerAlarm;
	}
	public int getGnnsAlarm() {
		return gnnsAlarm;
	}
	public void setGps(int gps) {
		this.gps = gps;
	}
	public void setGsm(int gsm) {
		this.gsm = gsm;
	}
	public void setAcc(int acc) {
		this.acc = acc;
	}
	public void setBattery(int battery) {
		this.battery = battery;
	}
	public void setPowerAlarm(int powerAlarm) {
		this.powerAlarm = powerAlarm;
	}
	public void setGnnsAlarm(int gnnsAlarm) {
		this.gnnsAlarm = gnnsAlarm;
	}
	public int getVoltage() {
		return voltage;
	}
	public int getSeqnum() {
		return seqnum;
	}
	public void setSeqnum(int seqnum) {
		this.seqnum = seqnum;
	}

	public int getConfirmAlarm() {
		return confirmAlarm;
	}

	public void setConfirmAlarm(int confirmAlarm) {
		this.confirmAlarm = confirmAlarm;
	}

	public Location getLoc() {
		return loc;
	}

	public void setLoc(Location loc) {
		this.loc = loc;
	}
	
	public int getLbsStatus() {
		return lbsStatus;
	}

	public void setLbsStatus(int lbsStatus) {
		this.lbsStatus = lbsStatus;
	}

	@Override
	public Object clone(){
		try {
			return (GpsStatus)super.clone();
		} catch (CloneNotSupportedException e) {
		}
		return null;
	}

    public int getBatteryVol() {
        return batteryVol;
    }

    public void setBatteryVol(int batteryVol) {
        this.batteryVol = batteryVol;
    }

    public int getMainVol() {
        return mainVol;
    }

    public void setMainVol(int mainVol) {
        this.mainVol = mainVol;
    }

	public int getTurnRightTimes() {
		return turnRightTimes;
	}

	public void setTurnRightTimes(int turnRightTimes) {
		this.turnRightTimes = turnRightTimes;
	}

	public int getTurnleftTimes() {
		return turnleftTimes;
	}

	public void setTurnleftTimes(int turnleftTimes) {
		this.turnleftTimes = turnleftTimes;
	}
    
}
